/*    */ package com.zimbra.cs.account.ldap;
/*    */ 
/*    */ import com.zimbra.common.util.Log;
/*    */ import com.zimbra.common.util.ZimbraLog;
/*    */ import java.io.PrintStream;
/*    */ import java.util.regex.Matcher;
/*    */ import java.util.regex.Pattern;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class LdapGalValueMap
/*    */ {
/*    */   private String mFieldName;
/*    */   private Pattern mRegex;
/*    */   private String mReplacement;
/* 29 */   private static Pattern sPattern = Pattern.compile("(\\S*): (\\S*) (.*)");
/*    */   
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */   LdapGalValueMap(String valueMap)
/*    */   {
/* 37 */     Matcher matcher = sPattern.matcher(valueMap);
/* 38 */     if (matcher.matches()) {
/* 39 */       this.mFieldName = matcher.group(1);
/* 40 */       this.mRegex = Pattern.compile(matcher.group(2));
/* 41 */       this.mReplacement = matcher.group(3);
/* 42 */       ZimbraLog.gal.debug("Gal value map: field=" + this.mFieldName + ", regex=" + this.mRegex + ", replacement=" + this.mReplacement);
/*    */     } else {
/* 44 */       ZimbraLog.gal.warn("unable to parse gal attr value map: " + valueMap);
/*    */     }
/*    */   }
/*    */   
/*    */   String getFieldName() {
/* 49 */     return this.mFieldName;
/*    */   }
/*    */   
/*    */   Object apply(Object value) {
/* 53 */     if ((value instanceof String))
/* 54 */       return replace((String)value);
/* 55 */     if ((value instanceof String[])) {
/* 56 */       String[] val = (String[])value;
/* 57 */       String[] newValue = new String[val.length];
/* 58 */       for (int i = 0; i < val.length; i++) {
/* 59 */         newValue[i] = replace(val[i]);
/*    */       }
/* 61 */       return newValue;
/*    */     }
/* 63 */     return value;
/*    */   }
/*    */   
/*    */   private String replace(String value) {
/* 67 */     if (this.mRegex == null) {
/* 68 */       return value;
/*    */     }
/* 70 */     Matcher matcher = this.mRegex.matcher(value);
/* 71 */     if (matcher.matches()) {
/* 72 */       return matcher.replaceAll(this.mReplacement);
/*    */     }
/* 74 */     return value;
/*    */   }
/*    */   
/*    */   public static void main(String[] ags) {
/* 78 */     LdapGalValueMap valueMap = new LdapGalValueMap("zimbraCalResType: [R|r]oom Location");
/*    */     
/* 80 */     Object newVal = valueMap.apply("Room");
/* 81 */     System.out.println((String)newVal);
/*    */     
/* 83 */     newVal = valueMap.apply(new String[] { "Room", "room" });
/* 84 */     for (String v : (String[])newVal) {
/* 85 */       System.out.println(v);
/*    */     }
/* 87 */     valueMap = new LdapGalValueMap("zimbraAccountCalendarUserType: Room|Equipment RESOURCE");
/* 88 */     newVal = valueMap.apply("Equipment");
/* 89 */     System.out.println((String)newVal);
/*    */   }
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/ldap/LdapGalValueMap.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */